package com.example.animaldoctor03;

import jsonjx.ChoiceAnimalJX;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;

public class MainActivity extends Activity {
	
	private String resultofmain = " ";
	private ListView listofstart = null ;
	private Button buttonofstart = null ;
	private HttpClient httpclientofmain = new DefaultHttpClient() ;
	private String Urlofmain  ="http://172.16.0.206/AnimalDoctor/queryAllAnimalInfoAction?appKey=b6d0027729f5c6a3";


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.fragment_main);
		listofstart = (ListView)findViewById(R.id.listofstart);
		listofstart.setOnItemClickListener(new listenerofstart());
		
		buttonofstart = (Button)findViewById(R.id.buttonofstart);
		buttonofstart.setOnClickListener(new listenerofstartbutton());
		
		postofmain();
		

		/*ChoiceAnimalJX choiceanimaljx2 = ChoiceAnimalJX.getInstance();
		
		String [] animalnameofmain =  choiceanimaljx2.getshuzuofmain();
		System.out.println("++++++++++++++++++++++++++=" + animalnameofmain[0]);*/
		
		
		
		//String [] animalnameofmain = {"猪"};
		//ArrayAdapter <String> arraAdapterofstart = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1, animalnameofmain);
		//listofstart.setAdapter(arraAdapterofstart);
		

		}
	class listenerofstartbutton implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intentofstart = new Intent (MainActivity.this , ZhengDuanFF.class);
			startActivity(intentofstart);
			finish();
			
		}
		
	}
	
	class listenerofstart implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intentofstart = new Intent (MainActivity.this , ZhengDuanFF.class);
			startActivity(intentofstart);
			finish();
			
		}	
	}
private	void postofmain(){
		new Thread(){
			public void run(){
	
	HttpPost httpPostofmain = new HttpPost(Urlofmain);
	httpPostofmain.setEntity(null);
	
	HttpParams httparamsofmain = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(httparamsofmain, 3000);
	
	try {
		HttpResponse httpResoponseofmain = httpclientofmain.execute(httpPostofmain);
		resultofmain = EntityUtils.toString(httpResoponseofmain.getEntity());
		
		
		System.out.println("&&&&&&&&&&&&&&&&" +  resultofmain);
		
		ChoiceAnimalJX choiceanimaljx = ChoiceAnimalJX.getInstance();
		choiceanimaljx.jxmain(resultofmain);
		

		//String []jxresultofmain =  choiceAnimalJX.jxmain(resultofmain);
		 //int lengthofreturnlist = choiceAnimalJX.getlistlengthofmain() ;

		
		/* for(int i = 0 ; i< lengthofreturnlist ; i++)
		 {
			 
		 }*/

		Message msgofmain = new Message();
		msgofmain.what = 1 ;
		handleofpostofmain.sendMessage(msgofmain);
		
		
		
		
		
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
			}
		}.start();
	
}

	
Handler handleofpostofmain = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(msg.what == 1){

				buttonofstart.setText("猪");
				
			}
	
		
		}
	};
	}

